package com.itheima.sc.impl.sms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.sms.SmsHomeNewProductApi;
import com.itheima.sc.mapper.sms.SmsHomeNewProductMapper;
import com.itheima.sc.pojo.sms.HomeBrand;
import com.itheima.sc.pojo.sms.HomeNewProduct;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeNewProductImpl implements SmsHomeNewProductApi {

    @Autowired
    private SmsHomeNewProductMapper newProductMapper;

    //添加新鲜好物的数据
    public void addNewProducts(List<HomeNewProduct> newProducts) {
        //遍历集合
        for (HomeNewProduct newProduct : newProducts) {
            newProductMapper.insert(newProduct);
        }
    }

    //批量删除好物新品
    public void deleteNewProducts(List<Long> ids) {
        newProductMapper.deleteBatchIds(ids);
    }

    //分页查询新品好物
    public PageResult selectNewProductList(Integer page, Integer pageSize, Integer recommendStatus, String productName) {
        //构建分页查询条件
        Page<HomeNewProduct> ipage = new Page<>(page, pageSize);
        //构建条件查询
        LambdaQueryWrapper<HomeNewProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(recommendStatus != null, HomeNewProduct::getRecommendStatus, recommendStatus);
        wrapper.like(StrUtil.isNotBlank(productName), HomeNewProduct::getProductName, productName);
        //排序
        wrapper.orderByDesc(HomeNewProduct::getSort);
        //查询请求
        ipage = newProductMapper.selectPage(ipage, wrapper);
        return new PageResult(page, pageSize, Convert.toInt(ipage.getTotal()),
                Convert.toInt(ipage.getTotal() / pageSize), ipage.getRecords());
    }

    //批量修改首页推荐的新鲜好物的状态
    public void updateNewProductStatus(List<Long> ids, Integer status) {
        for (Long id : ids) {
            HomeNewProduct newProduct = new HomeNewProduct();
            newProduct.setId(id);
            newProduct.setRecommendStatus(status);
            newProductMapper.updateById(newProduct);
        }
    }

    //修改推荐好物排序
    public void updateNewProductSort(HomeNewProduct newProduct) {
        newProductMapper.updateById(newProduct);
    }
}
